package com.zklh.leetcode.other.problem17;

import java.util.ArrayList;
import java.util.List;

public class Solution {

    // 用于盛放表示数字对应起始字母, 索引号为数字-1
    private char[] charsTable = new char[8];
    private List<String> result = new ArrayList<>();
    // 初始化charsTable
    {
        char c = 'a';
        int step = 3;
        for (int i = 0; i < 8; i++) {
            charsTable[i] = c;
            if(i == 5 || i == 7){
                step = 4;
            }else{
                step = 3;
            }
            c+=step;
        }
    }
    public List<String> letterCombinations(String digits) {
        char[] numChars = digits.toCharArray();
        if(digits.length() == 0){
            return result;
        }
        appendChar(numChars,0,numChars.clone());
        return result;
    }

    /**
     * numChars 原本盛放数字字符串的字符集, 递归调用的时候, 盛放的是代表字母的字符集
     * @param numChars
     * @param loc 表示当前将要盛放字符的在字符串中的位置索引
     */
    private void appendChar(char[] numChars, int loc, char[] strChars){
        if(loc == numChars.length){
            result.add(new String(strChars));
            return;
        }
        // 获取当前的数字-1
        int num = numChars[loc]-50;
        // 可选字符的下限 a - z
        char down = charsTable[num];
        // 可选字符的上限
        char up = num+1 > 7 ? ('z'+1) : charsTable[num+1];
        while(down < up){
            strChars[loc]=down;
            appendChar(numChars,loc+1,strChars);
            down++;
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        solution.letterCombinations("23");
        System.out.println("23");
    }
}
